package com.foreveross.crawl.group;



public class TaskGroupManager {/*

	private static TaskGroupManager manager=null;
	
	private static int registerMaxSize=0;
	private static int doMaxSize=0;
	private static double allowMaxMissing=1.0;//允许的组任务最大的渠道缺失率，默认为1，100%
	
	private static BlockingQueue<TaskGroup> doGroups=null;
	private static BlockingQueue<TaskGroup> waitGroups=null;
	
	private static Logger logger = org.slf4j.LoggerFactory.getLogger(TaskGroupManager.class);
	private TaskGroupManager() {
		super();
	}

	
	*//**
	 * 任务组管理器初始化
	 * @param registerMaxSize
	 * @param doMaxSize
	 * @throws Exception
	 *//*
	public static void init(int registerMaxSize,int doMaxSize,double allowMaxMissing) throws Exception{
		if(manager!=null){
			throw new Exception("The TaskGroupManager Has Init!");
		}
		manager=new TaskGroupManager();
		TaskGroupManager.registerMaxSize=registerMaxSize>=0?registerMaxSize:0;
		TaskGroupManager.doMaxSize=doMaxSize>=0?doMaxSize:0;
		waitGroups=new ArrayBlockingQueue<TaskGroup>(registerMaxSize);
		doGroups=new ArrayBlockingQueue<TaskGroup>(doMaxSize);
		TaskGroupManager.allowMaxMissing=allowMaxMissing>=1?1:(allowMaxMissing<=0?0:allowMaxMissing);
		logger.info(String.format("任务组管理器 初始完毕！允许注册任务组最大数量[%s] 允许最大组任务组并发数量[%s] 允许任务组渠道最高缺失率[%s]",
				TaskGroupManager.registerMaxSize,TaskGroupManager.doMaxSize,(TaskGroupManager.allowMaxMissing*100)+"%"));
	}
	public static TaskGroupManager getInstance(){
		return manager;
	}
	
	*//**
	 * 注册一个任务组(如果注册成功返回true)
	 * @param groupName
	 * @param from
	 * @param to
	 * @param flightDate
	 * @param timeout
	 * @return
	 * @throws Exception 
	 *//*
	public boolean registerTaskGroup(String groupName,String from,String to,String flightDate,long timeout) throws Exception{	
		if(StringUtils.isEmpty(groupName) || StringUtils.isEmpty(from) || StringUtils.isEmpty(to) || StringUtils.isEmpty(flightDate)){
			throw new Exception("has field null!");
		}
		TaskGroup tg=new TaskGroup(groupName, from, to, flightDate, timeout);
		if(waitGroups.contains(tg)){
			logger.info(String.format("注册任务组 [%s] 失败  原因：已存在！", groupName));
			throw new ExistRegisterGroupException();
		}
		if(!waitGroups.offer(tg)){
			throw new PassRegisterGroupSizeException();
		}else{
			logger.info(String.format("注册任务组[%s]", groupName));
		}
		return true;
	}
	*//**
	 * 注册一串任务组，当管理器数量超过最大值的时候，就不再注册
	 * @param tgs
	 * @return
	 * @throws Exception
	 *//*
	public void registerTaskGroups(List<TaskGroup> tgs){
		if(tgs==null || tgs.isEmpty()){
			return;
		}
		for(TaskGroup tg:tgs){
			if(tg==null){break;}
			if(waitGroups.contains(tg)){continue;}
			if(waitGroups.offer(tg)){
				logger.info(String.format("注册任务组[%s]", tg.getGroupName()));
			}else{
				return;
			}
		}
	}
	
	
	public TaskGroup buildTaskGroup(String groupName,String from,String to,String flightDate,long timeout)throws Exception{
		if(StringUtils.isEmpty(groupName) || StringUtils.isEmpty(from) || StringUtils.isEmpty(to) || StringUtils.isEmpty(flightDate)){
			throw new Exception("has field null!");
		}
		return new TaskGroup(groupName, from, to, flightDate, timeout);
	}
	
	
	
	
	
	*//**
	 * 获得注册任务组数量
	 * @return
	 * @throws Exception
	 *//*
	public int getRegisterTaskGroupSize()throws Exception{
		return waitGroups.size();
	}
	
	
	*//**
	 * 拿到一个已经注册的任务组，同时会移除等待序列
	 * @return
	 *//*
	public TaskGroup getOneRsgisterTaskGroup() throws Exception{
		TaskGroup tg=waitGroups.poll();
		if(tg==null){
			throw new NoneRegisterGroupException();
		}
		return tg;
	}
	
	
	
	*//**
	 *  讲一个任务组加入抓取队列 ，指定超时时间，同时会改变任务组的开始时间为now,指定任务组的执行渠道，不能为空
	 * @param group
	 * @param channels
	 * @param timeout
	 * @return
	 * @throws Exception 
	 *//*
	public boolean joinCrawlTaskGroup(TaskGroup group,List<GradChannelEntity> channels,long timeout) throws Exception{
		if(channels==null || channels.isEmpty()){
			throw new Exception("channels is null!");
		}
		if(doGroups.contains(group)){
			throw new ExistDoGroupException();
		}
		if(!doGroups.offer(group)){
			throw new PassDoGroupSizeException();
		}else{
			logger.info(String.format("任务组[%s]加入抓取队列", group.getGroupName()));
		}
		logger.info(String.format("当前任务组数量[%s]", doGroups.size()));
		//group.setStartTime(System.currentTimeMillis());
		group.setTimeout(timeout);
		group.setChannels(channels);
		return true;
	}
	*//**
	 * 设置任务组开始时间
	 * @return
	 *//*
	public synchronized boolean setGroupStartTime(TaskGroup g,boolean isOverWrite){
		if(g==null){return false;}
		if(isOverWrite){
			g.setStartTime(System.currentTimeMillis());
			return true;
		}else if(g.getStartTime()<=0){
			g.setStartTime(System.currentTimeMillis());
			return true;
		}
		return false;
	}
	
	
	*//**
	 * 讲一个任务组移除抓取队列
	 * @param group
	 * @return
	 *//*
	public boolean removeCrawlTaskGroup(TaskGroup group){
		if(doGroups.remove(group)){
			logger.info(String.format("成功移除任务组[%s]", group.getGroupName()));
			logger.info(String.format("当前任务组数量[%s]", doGroups.size()));
			return true;
		}
		return false;
	}
	*//**
	 * 移除超时的任务组
	 * @return
	 *//*
	public List<TaskGroup> findTimeoutAndRemove(){
		List<TaskGroup> result=new ArrayList<TaskGroup>();
		for(TaskGroup g:doGroups){
			if(g.isTimeout()){
				doGroups.remove(g);
				result.add(g);
			}
		}
		return result;
	}
	*//**
	 * 通知任务组打的某一个渠道已经完成
	 * @param tg
	 * @param channel
	 * @return
	 *//*
	public  boolean  notifyOneChannelDoneInGroup(TaskGroup tg,GradChannelEntity channel){
		boolean flag=tg.getChannels().remove(channel);
		if(flag){
			logger.info(String.format("移除任务组[%s]中渠道[%s]", tg.getGroupName(),channel.getName()));
		}
		if(tg.getChannels().isEmpty() && doGroups.remove(tg)){
			logger.info(String.format("组管理器移除任务组[%s]",tg.getGroupName()));
			logger.info(String.format("当前任务组数量[%s]", doGroups.size()));
		}
		return flag;
	}
	
	
	*//**
	 * 在做任务之前检测下组任务的渠道缺失率，如果超过允许的最大值，则抛出异常
	 * 如果不超过最大的渠道缺失率，则返回过滤后的可以继续打开抓取线程的渠道
	 * 缺失率=1-未达到最大并发量的可运行的渠道/组任务所有的渠道数量
	 * 每个渠道线程所在的组标准格式 CrawlContext.CRAWL_TASK_CHANNEL_GROUP+channel.getId()
	 * @param channels
	 * @return
	 * @throws Exception 
	 *//*
	public List<GradChannelEntity> filterMissingProportion (List<GradChannelEntity> channels,Scheduler scheduler ) throws PassMaxMissingException,Exception{
		List<GradChannelEntity> result=new ArrayList<GradChannelEntity>();
		if(channels==null || channels.isEmpty()){throw new Exception("channel is null!");}
		for(GradChannelEntity channel:channels){
			if(!ChannelComplicatingDataManager.getInstance().isReachComplicatingSize(channel, scheduler)){
				result.add(channel);
			}
		}
		double q= 1-(double)result.size()/(double)channels.size();
		logger.info(String.format("渠道缺失率[%s]",q));
		if(q>allowMaxMissing){
			throw new PassMaxMissingException();
		}
		return result;
	}
	
	public void clearRegisterGroup(){
		waitGroups.clear();
		logger.info("等待中任务组清空");
	}
	
	
	
	public static void main(String[] args) {
		System.out.println(StringUtils.isEmpty(" "));
	}

	
	
*/}
